# -*- coding: utf-8 -*-
import base_object
import t_cmd
import cg_network
import cPickle
import t_room_logic
import t_mapmgr, t_const

inst = None

#模块初始化函数
def init():
	global inst
	if not inst:
		inst = CCommand()

#模块自清理函数，释放引用
def destroy():
	global inst
	if inst:
		inst.destroy()
		inst = None

#网络消息打包和发送的类，所有客户端向服务端发送的消息都在这里打包和发送
class CCommand(base_object.CObject):
	def __init__(self):
		super(CCommand, self).__init__()

	#自清理函数，因为这个对象没有成员变量，所有也不需要释放什么东西
	def destroy(self):
		pass

	#选择关卡消息的打包发送函数：iStage为关卡的id
	def send_select_stage(self, iStage):
		cg_network.sender.game_c_select_stage(stage=iStage)

	#准备/取消准备消息的打包发送函数：flag=1代表准备完毕，flag=0代表取消准备
	def send_game_ready(self, flag):
		cg_network.sender.game_c_game_ready(is_ready=flag)

	#离开房间消息的打包发送函数：无参数
	def send_leave_game(self):
		cg_network.sender.game_c_leave_game(no_use=0)
	#单机用的结束游戏
	def send_game_end(self,flag):
		cg_network.sender.game_c_game_end(iswin=flag)

	#踢人
	def send_game_kick(self,obj):
		cg_network.sender.game_c_game_kick(hid=obj.hid)
	#换座位
	def send_game_side(self,iuid,old,new):
		cg_network.sender.game_c_game_side(uid=iuid,old_side=old,new_side=new)

	#换房主
	def send_game_master(self,imaster):
		cg_network.sender.game_c_game_master(master=imaster)

	#巫师指令的打包发送函数：cmds是巫师指令id；param_list是具体的指令信息（根据id不同，长度可变的int列表）
	def send_wizard(self, cmds, param_list):
		int_list = []
		for key in param_list:
			obj = cg_network.get_game_room_msgmgr().game_single_int(value=key)
			int_list.append(obj)
		cg_network.sender.game_c_wizard(head=cmds, param=int_list)

	#游戏初始化ok
	def send_game_init(self, flag):
		print "send_game_init_deno!"
		cg_network.sender.game_c_game_init(binit=flag)

	#游戏内子事件的打包发送函数：event是子事件的id；param_list是子事件的具体内容
	def send_game_event(self, event, param_list):
		_str = cPickle.dumps(param_list)
		cg_network.sender.game_c_game_event(frame=0, event_id=event, param=_str)
		print  event, _str
#-------------------------发送消息给服务器

	#玩家选择角色
	def send_choose(self,model):
		param = (model,)
		self.send_game_event(t_cmd.C_CHOOSE,param)
	# 玩家发送移动
	def send_move(self):
		param = ()
		self.send_game_event(t_cmd.C_MOVE, param)
	# 玩家停止点
	def send_stop(self,street,numb):
		param = (street,numb)
		self.send_game_event(t_cmd.C_STOP, param)
	# 玩家对谁使用什么锦囊
	def send_skills(self,skill,target):
		param = (skill,target)
		self.send_game_event(t_cmd.C_SKILLS, param)




#----------------------------------------------------------------------